#include "level.hpp"

Level::Level(App* appPtr) : m_app(appPtr)
{
	m_map = NULL;
}


void Level::Load(std::string pathLevel)
{
	m_fileName = pathLevel;

	LoadBackgrounds();
	LoadMap();
	LoadForegrounds();
    LoadItems();

}

void Level::LoadMap()
{

    // Get Map Path
    std::string path = FileParser::ValueToString(m_fileName, "Map");

    if(path != "")
	{
		m_map = new Background(&m_app->m_ressourceManager);
		m_map->Load(path, MAP);
    }
}

void Level::LoadMusic()
{
    // Get Music Path
    std::string path = FileParser::ValueToString(m_fileName, "Music");

    if(path != "")
    {

    }
}

void Level::LoadBackgrounds()
{

    std::queue<TagValues> values;

    FileParser::AllTagValues(m_fileName, "AddBackground", values);

    while(values.size() != 0)
    {
        Background* backgroundPtr = new Background(&m_app->m_ressourceManager);
        backgroundPtr->Load(values.front().second.at(0), BACKGROUND);
        m_listBackground.push_back(backgroundPtr);
        values.pop();
    }

    /*// Open Map File
	DK_File MapFile(m_fileName, FILE_LEVEL);

	std::cout << "Maximun Size Supported : " << ImageRessource::MaximunSize() << std::endl;

	for(int i = 0; i < 4; i++)
	{
	  Background* backgroundPtr = NULL;

		if(i == 0)
		{
			if(MapFile.seek_word("Background") != "")
			{
				backgroundPtr = new Background(&m_app->m_ressourceManager);
				//std::cout << MapFile.seek_word("Background");
				backgroundPtr->Load(MapFile.seek_word("Background"), BACKGROUND);

				m_listBackground.push_back(backgroundPtr);
			}
		}
		else
		{
			std::ostringstream oss;
			oss << i;

			if(MapFile.seek_word("Background" + oss.str()) != "")
			{
				backgroundPtr = new Background(&m_app->m_ressourceManager);
				backgroundPtr->Load(MapFile.seek_word("Background" + oss.str()), BACKGROUND);
				m_listBackground.push_back(backgroundPtr);
			}

	   }

    }*/

}

void Level::LoadForegrounds()
{
	// Open Map File
	DK_File MapFile(m_fileName, FILE_LEVEL);

	for(int i = 0; i < 4; i++)
	{
	  Background* backgroundPtr = NULL;

		if(i == 0)
		{
			if(MapFile.seek_word("Foreground") != "")
			{
				backgroundPtr = new Background(&m_app->m_ressourceManager);
				//std::cout << MapFile.seek_word("Background");
				backgroundPtr->Load(MapFile.seek_word("Foreground"), FOREGROUND);

				m_listForeground.push_back(backgroundPtr);
			}
		}
		else
		{
			std::ostringstream oss;
			oss << i;

			if(MapFile.seek_word("Foreground" + oss.str()) != "")
			{
				backgroundPtr = new Background(&m_app->m_ressourceManager);
				backgroundPtr->Load(MapFile.seek_word("Foreground" + oss.str()), FOREGROUND);
				m_listForeground.push_back(backgroundPtr);
			}

	   }

	}
}

void Level::LoadItems()
{
    std::queue<TagValues> values;

    FileParser::AllTagValues(m_fileName, "AddItem", values);

    while(values.size() != 0)
    {
        Item* itemPtr = ScriptLoader::GetScriptLoader()->CopyItem(FileParser::ToInt(values.front().second.at(0)));
        itemPtr->GetAnimation()->SetPosition(FileParser::ToInt(values.front().second.at(1)), FileParser::ToInt(values.front().second.at(2)));

        m_listItem.push_back(itemPtr);

        itemPtr = NULL;
        values.pop();
    }

}

void Level::Scrolling(int view_X,int Carte_X)
{
    for(int i = 0;i < m_listBackground.size();i++)
    {
        m_listBackground[i]->Scrolling(view_X,Carte_X);
    }
    for(int i = 0;i < m_listForeground.size();i++)
    {
        m_listForeground[i]->Scrolling(view_X,Carte_X);
    }
}

sf::Vector2f Level::SpawnPosition()
{
    // Open Map File
	DK_File MapFile(m_fileName, FILE_LEVEL);

	m_posPlayer.x = FileParser::ValueToInt(m_fileName,"Personnage_X");
	m_posPlayer.y = FileParser::ValueToInt(m_fileName,"Personnage_Y");
	std::cout << "Position de depart X : " << FileParser::ValueToInt(m_fileName,"Personnage_X") << " Y : " << FileParser::ValueToInt(m_fileName,"Personnage_Y") << std::endl;

	return m_posPlayer;
}

Background* Level::Map()
{
	return m_map;
}

std::vector<Background*>& Level::Backgrounds()
{
	return m_listBackground;
}

std::vector<Background*>& Level::Foregrounds()
{
	return m_listForeground;
}

std::vector<Item*>& Level::Items()
{
    return m_listItem;
}

void Level::AddItem(Item *itemPtr)
{

    m_listItem.push_back(itemPtr);
 }
